<?php
include("../models/entities/class.AccountEntity.php");
@session_start();
if (isset($_SESSION["Username"])) {
    include("../models/webconfig.php");
    include("../models/class.connection.php");
    include("../models/entities/class.OrderEntity.php");
    include("../models/repositories/class.OrderRepository.php");
    include("../library/library.pager.php");

    include("../models/repositories/class.GroupAuthorityRepository.php");
    if (isset($_SESSION['Account'])) {
        $groupAuthorityRepository = new GroupAuthorityRepository();
        $account = $_SESSION['Account'];
        if (!$groupAuthorityRepository->CheckExistGroupAuthority($account->GroupID_i, 3)) {
            header("location:NoAccess.php");
        }
    } else {
        header("location:NoAccess.php");
    }

    $orderRepository = new OrderRepository();
    $totalRecords = 0;
    $currentPage = 1;
    $itemsPerPage = 5;
    $pageSize = 5; //Số items thực tế / trang hiện hành
    if (isset($_GET['page'])) {
        if (is_numeric($_GET['page']))
            $currentPage = $_GET['page'];
    }
    $result = $orderRepository->GetPager($currentPage, $itemsPerPage, $totalRecords);
    $generateLink = GenerateLink($totalRecords, $pageSize, $itemsPerPage, $currentPage);
    $index = 0;
?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

        <head>
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
            <title>Trang quản trị WebFlower</title>
            <link href="../content/Layout.css" media="screen" rel="stylesheet" type="text/css" />
            <link href="../content/orderManager.css" rel="stylesheet" type="text/css" />
            <script src="../scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
            <link href="../content/ui-lightness/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" media="screen"/>
            <script type="text/javascript" src="../scripts/jquery-ui-1.8.6.custom.min.js"></script>
            <script type="text/javascript" src="../scripts/jquery.ui.datetimepicker.min.js"></script>
            <script src="../scripts/adminorder.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(document).ready(function () {
                    var str = location.href.toLowerCase();

                    $('ul#mainNav li a').each(function () {
                        if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
                            $(this).addClass("active");
                    });

                    $('ul#sideNav li a').each(function () {
                        if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
                            $(this).addClass("active");
                    });

                    MainScript.init();    
                    $("#ui-datepicker-div").hide();
                });

            </script>
        </head>

        <body>

            <div id="format">
                <div id="wrapper">
                    <!--Phần main menu--><?php
    include("../includes/backend_mainnav.php");
?>
                    <div id="containerHolder">
                        <div id="container">
                            <div id="maincontent">
                                <h3>Quản lý đơn đặt hàng</h3>
                                <center>
                                    <h4>Tìm kiếm theo </h4>
                                    <div id="searchby">
                                        <div class="searchItem">
                                            <span>Trạng thái : </span><span>Chưa duyệt
                                                <input id="check" checked="checked" name="rdState" type="radio" value="yes" /></span>
                                            <span>Đã duyệt<input id="rdState" name="rdState" type="radio" value="no" /></span>
                                            <input id="searchByCheck" type="button" value="Tìm kiếm" /></div>
                                        <div class="searchItem">
                                            <span>Thời gian đặt hàng: </span><span>Từ ngày
                                                <input id="dateBeginOrder" name="dateBeginOrder" type="text" value="&lt;--Ngày bắt đầu--&gt;"/>
                                            </span><span>Đến ngày
                                                <input id="dateEndOrder" name="dateEndOrder" type="text" value="&lt;--Ngày kết thúc--&gt;" />
                                            </span><input id="searchByOrderDate" type="button" value="Tìm kiếm" /></div>

                                        <div class="searchItem">
                                            <span>Thời gian giao hàng: </span><span>Từ ngày
                                                <input id="dateBeginDeli" name="dateBeginDeli" type="text" value="&lt;--Ngày bắt đầu--&gt;" />
                                            </span><span>Đến ngày
                                                <input id="dateEndDeli" name="dateEndDeli" type="text" value="&lt;--Ngày kết thúc--&gt;" />
                                            </span><input id="searchByDeliDate" type="button" value="Tìm kiếm" /></div>
                                        <div class="searchItem">
                                            <span>Email khách hàng: </span><span>Nhập email
                                                <input id="email" name="email" type="text" value="&lt;--Email--&gt;" />
                                            </span><input id="searchByEmail" type="button" value="Tìm kiếm" /></div>
                                        <div class="searchItem">
                                            <span>Số điện thoại khách hàng: </span><span>Nhập số
    							điện thoại
                                                <input id="phone" name="phone" type="text" value="&lt;--Điện thoại--&gt;" />
                                            </span><input id="searchByPhone" type="button" value="Tìm kiếm" /></div>
                                    </div>
                                    <h4>Danh sách đơn đặt hàng</h4>
                                    <div id="contentOrders">
                                        <div class="loading"></div>

                                        <table id="lstOrders" cellspacing="0">
                                            <tr>
                                                <td class="title">STT</td>
                                                <td class="title">Mã ĐH</td>
                                                <td class="title">Tên KH</td>
                                                <td class="title">Email</td>
                                                <td class="title">Phone</td>
                                                <td class="title">Thời gian đặt hàng</td>
                                                <td class="title">Thời gian giao hàng</td>
                                                <td class="title">Trạng thái</td>
                                                <td class="title" colspan="2">Chức năng</td>
                                            </tr>
<?php if (count($result) > 0) { ?>
<?php for ($i = 0; $i < count($result); $i++) {
            $index++; ?>
                                        <?php if ($result[$i]->Checked_b == 0) {
                                        ?>
                                            <tr id="tr-<?php echo($result[$i]->ID_i); ?>">
                                                <td style="width: 49px"><?php echo($index); ?></td>
                                                <td><?php echo($result[$i]->ID_i); ?></td>
                                                <td><?php echo($result[$i]->CustomerName_s); ?></td>
                                                <td><?php echo($result[$i]->Email_s); ?></td>
                                                <td><?php echo($result[$i]->Phone_s); ?></td>
                                                <td><?php echo(substr($result[$i]->OrderTime_dt, 0, 10)); ?></td>
                                                <td><?php echo(substr($result[$i]->DeliveryTime_dt, 0, 10)); ?></td>
                                                <td><input class="checked" orderId="<?php echo($result[$i]->ID_i); ?>" type="checkbox" />
                                                </td>
                                                <td><a href="#" onclick="return false;" class="detail" orderId="<?php echo($result[$i]->ID_i); ?>">Xem</a></td>
                                                <td><a href="#" onclick="return false;" class="delete" orderId="<?php echo($result[$i]->ID_i); ?>">Xóa</a></td>
                                            </tr>
<?php } else { ?>
                                            <tr style="background-color: #f0f7fa;" id="tr-<?php echo($result[$i]->ID_i); ?>">
                                                <td style="width: 49px"><?php echo($index); ?></td>
                                                <td><?php echo($result[$i]->ID_i); ?></td>
                                                <td><?php echo($result[$i]->CustomerName_s); ?></td>
                                                <td><?php echo($result[$i]->Email_s); ?></td>
                                                <td><?php echo($result[$i]->Phone_s); ?></td>
                                                <td><?php echo(substr($result[$i]->OrderTime_dt, 0, 10)); ?></td>
                                                <td><?php echo(substr($result[$i]->DeliveryTime_dt, 0, 10)); ?></td>
                                                <td><input class="checked" orderId="<?php echo($result[$i]->ID_i); ?>" type="checkbox" checked="checked"/>
                                                </td>
                                                <td><a href="#" onclick="return false;" class="detail" orderId="<?php echo($result[$i]->ID_i); ?>">Xem</a></td>
                                                <td><a href="#" onclick="return false;" class="delete" orderId="<?php echo($result[$i]->ID_i); ?>">Xóa</a></td>
                                            </tr>
<?php } ?>

<?php } ?>
<?php } ?>
                                    </table>
                                    <div class="pagination pagination-left">
                                        <div class="results">
                                            <span>
                                                Kết quả từ <?php echo (($currentPage - 1) * $itemsPerPage + (count($result) > 0 ? 1 : 0)); ?> - <?php echo (($currentPage - 1) * $itemsPerPage + count($result)); ?> của <?php echo($totalRecords); ?>
                                            </span>
                                        </div>
                                        <ul class="pager"><?php echo $generateLink ?></ul>
                                    </div>
                                </div>
                            </center></div>
                    </div>

                    <!-- // #main --></div>
                <div id="dialog" title="Chi tiết đơn đặt hàng"></div>
                <div id="messagebox" title=" Xác nhận "></div>
                <!-- // #container -->
                <!--Phần footer của trang--><?php
                                        include("../includes/backend_footer.php");
?></div>
        </div>

    </body>

</html>
<?php
                                    } else {
                                        header("location:../views/Admin_login.php?logout=TRUE");
                                    }
?>
